package com.wl156.activities.publish;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.activities.find.GoodInfo;
import com.wl156.activities.find.LogisticCompanyInfo;
import com.wl156.activities.find.SpecialLine;
import com.wl156.adapters.LogisticCompanyAdapter;
import com.wl156.adapters.SpecialLineAdapter;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;

public class NewInfoAfterBase extends BaseActivity implements
		android.view.View.OnClickListener {

	private static final String TAG = null;
	protected TextView mTipView;
	protected ListView mListView;
	protected Button mSendMessage;

	private ListView mListSpecial;
	private ListView mListCompany;

	protected ViewStub mHeaderStub;

	protected WebServiceClient mWebServiceClient;
	protected Certificate mCertificate;

	String startLocationLv1;
	String startLocationLv2;
	String endLocationLv1;

	RadioButton goodInfo;
	RadioButton specialLineInfo;
	RadioButton companyInfo;

	LinearLayout goodLayout;
	LinearLayout specialLayout;
	LinearLayout companyLayout;

	protected Class getGoodOrCarInfoClass() {
		return GoodInfo.class;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_new_good_info_after);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		goodInfo = (RadioButton) findViewById(R.id.goodInfo);
		goodInfo.setOnClickListener(this);
		specialLineInfo = (RadioButton) findViewById(R.id.specialLineInfo);
		specialLineInfo.setOnClickListener(this);
		companyInfo = (RadioButton) findViewById(R.id.companyInfo);
		companyInfo.setOnClickListener(this);

		goodLayout = (LinearLayout) findViewById(R.id.goodInfoLayout);
		specialLayout = (LinearLayout) findViewById(R.id.specialLineLayout);
		companyLayout = (LinearLayout) findViewById(R.id.companyLayout);

		mListView = (ListView) findViewById(R.id.list);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(NewInfoAfterBase.this,
						getGoodOrCarInfoClass());
				i.putExtra("id", id);
				startActivity(i);
			}
		});

		mHeaderStub = (ViewStub) findViewById(R.id.header_stub);
		mTipView = (TextView) findViewById(R.id.tip);
		mListView = (ListView) findViewById(R.id.list);
		mSendMessage = (Button) findViewById(R.id.send_message);
		mSendMessage.setOnClickListener(this);

		mListSpecial = (ListView) findViewById(R.id.list_special);
		mListSpecial.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(NewInfoAfterBase.this, SpecialLine.class);
				i.putExtra("id", id);
				startActivity(i);
			}
		});
		mListCompany = (ListView) findViewById(R.id.list_company);
		mListCompany.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(NewInfoAfterBase.this,
						LogisticCompanyInfo.class);
				i.putExtra("id", id);
				startActivity(i);
			}
		});

		Bundle extras = getIntent().getExtras();
		startLocationLv1 = extras.getString("startLocationLv1");
		startLocationLv2 = extras.getString("startLocationLv2");
		endLocationLv1 = extras.getString("endLocationLv1");

		new FindTask().execute();
		new FindCompanyTask().execute();
	}

	@Override
	public void onContentChanged() {
		super.onContentChanged();

		mHeaderStub = (ViewStub) findViewById(R.id.header_stub);
	}

	public class FindTask extends AsyncTask<Void, Void, Object> {
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);

			try {
				if (result instanceof SoapObject) {
					SoapObject so = (SoapObject) result;
					if (so.getPropertyCount() == 0) {
					} else if (so.getPropertyCount() == 1
							&& ((SoapObject) so.getProperty(0))
									.getPropertyCount() == 0) {
						SoapObject so1 = (SoapObject) so.getProperty(0);
						if (so1.getPropertyCount() == 0) {
						}
					} else {
						Log.d(TAG, "findresultbase adapter else");

						SpecialLineAdapter adapter = new SpecialLineAdapter(
								NewInfoAfterBase.this, (SoapObject) result);
						mListSpecial.setAdapter(adapter);
					}
				} else {
				}
			} catch (Exception e) {
			}
		}

		@Override
		protected Object doInBackground(Void... params) {
			try {
				return mWebServiceClient.commonFindSpecialLines(mCertificate,
						startLocationLv1, startLocationLv2, "", endLocationLv1,
						"", "", "", "", "", "", 0, 10, "");
			} catch (IOException e) {
				return e;
			} catch (XmlPullParserException e) {
				return e;
			}
		}
	}

	public class FindCompanyTask extends AsyncTask<Void, Void, Object> {
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);

			try {
				LogisticCompanyAdapter adapter = new LogisticCompanyAdapter(
						NewInfoAfterBase.this, (SoapObject) result);
				mListCompany.setAdapter(adapter);
			} catch (Exception e) {
			}
		}

		@Override
		protected Object doInBackground(Void... params) {
			try {
				return mWebServiceClient.commonFindLogisticCompanys(
						mCertificate, startLocationLv1, startLocationLv2, "",
						"", "", "", "", 0, 10);
			} catch (IOException e) {
				return e;
			} catch (XmlPullParserException e) {
				return e;
			}
		}
	}

	@Override
	public void onBackPressed() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("退出本页，你将不能再进入，是否退出？");
		builder.setPositiveButton("退出", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				NewInfoAfterBase.super.onBackPressed();
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
		builder.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.goodInfo: {
			goodLayout.setVisibility(View.VISIBLE);
			specialLayout.setVisibility(View.GONE);
			companyLayout.setVisibility(View.GONE);

			// if (getGoodOrCarInfoClass() == GoodInfo.class) {
			// mHeaderStub
			// .setLayoutResource(R.layout.item_good_header);
			// } else {
			// mHeaderStub
			// .setLayoutResource(R.layout.item_truck_message_header);
			// }
			// mHeaderStub.inflate();
		}
			break;
		case R.id.specialLineInfo: {
			goodLayout.setVisibility(View.GONE);
			specialLayout.setVisibility(View.VISIBLE);
			companyLayout.setVisibility(View.GONE);

			// mHeaderStub.setLayoutResource(R.layout.item_special_line_header);
			// mHeaderStub.inflate();
		}
			break;
		case R.id.companyInfo: {
			goodLayout.setVisibility(View.GONE);
			specialLayout.setVisibility(View.GONE);
			companyLayout.setVisibility(View.VISIBLE);

			// mHeaderStub
			// .setLayoutResource(R.layout.item_logistic_company_header);
			// mHeaderStub.inflate();
		}
			break;
		case R.id.send_message: {
			try {
				StringBuilder numbers = new StringBuilder();
				int count = mListView.getAdapter().getCount();
				for (int i = 0; i < count; i++) {
					SoapObject so = (SoapObject) mListView.getAdapter()
							.getItem(i);
					String phone = Utils.propertyToShow(so.getProperty(9));
					numbers.append(phone);
					numbers.append(";");
				}
				Uri uri = Uri.parse("smsto:" + numbers.toString());
				Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
				intent.putExtra("sms_body", "The SMS text");
				startActivity(intent);
			} catch (Exception e) {
			}
		}
			break;
		}
	}
}
